package com.yige.web.controller

import com.mongodb.DBObject
import com.yige.common.ArgsUtil
import com.yige.common.ResultUtil
import com.yige.common.ToDBObjectUtil
import com.yige.service.mongoService.MongoActionExpenditureService
import com.yige.service.mongoService.MongoActionIncomeService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

/**
 * Created by sunhao on 2015/9/29 0029.
 */

@Controller
@RequestMapping("manage")
class ActionController {

    @Autowired
    MongoActionIncomeService actionIncomeService
    @Autowired
    MongoActionExpenditureService actionExpenditureService



    /*************控制器模块************/

    /**
     * 收入设置
     * @param model
     * @return
     */
    @RequestMapping("income.do")
    def incomeList(Model model){
        List<DBObject> incomeList = actionIncomeService.findList()
        model.addAttribute("incomes",incomeList)
        return "action/incomeList"
    }

    /**
     * 添加收入项目
     * @param name
     */
    @RequestMapping("addIncome")
    @ResponseBody
    def addIncome(String name, String item ){
        Map<String,DBObject> actionInfo = [_id:UUID.randomUUID().toString(),name:name,item:item]
        DBObject dbObject = ToDBObjectUtil.toDBObject(actionInfo)
        boolean flag = saveIncome(dbObject)
        ResultUtil.recallAjax(flag)
    }

    /**
     * 删除收入项目
     * @param _ids
     * @return
     */
    @RequestMapping("delIncome")
    @ResponseBody
    def delIncome(@RequestParam("_ids") List<String> _ids){
        boolean flag = false
        _ids.each {_id->
            flag = actionIncomeService.deleteOne(_id)
        }
        ResultUtil.recallAjax(flag)
    }

    /**
     * 修改收入项目
     * @param _id
     * @param name
     * @return
     */
    @RequestMapping("updateIncome")
    @ResponseBody
    def updateIncome(String _id, String name, String item){
        Map<String,DBObject> actionInfo = [_id:_id,name:name,item:item]
        DBObject dbObject = ToDBObjectUtil.toDBObject(actionInfo)
        boolean flag = actionIncomeService.updateOne(_id,dbObject)
        ResultUtil.recallAjax(flag)
    }

    /**
     * 支出设置
     * @param model
     * @return
     */
    @RequestMapping("expend.do")
    def expendList(Model model){
        List<DBObject> expendList = actionExpenditureService.findList()
        model.addAttribute("expends",expendList)
        return "action/expendList"
    }

    /**
     * 添加支出项目
     * @param name
     * @return
     */
    @RequestMapping("addExpend")
    @ResponseBody
    def addExpend(String name, String item){
        Map<String,DBObject> actionInfo = [_id:UUID.randomUUID().toString(),name:name,item: item]
        DBObject dbObject = ToDBObjectUtil.toDBObject(actionInfo)
        boolean flag = saveExpend(dbObject)
        ResultUtil.recallAjax(flag)
    }

    /**
     * 删除支出项目
     * @param _ids
     * @return
     */
    @RequestMapping("delExpend")
    @ResponseBody
    def delExpend(@RequestParam("_ids") List<String> _ids){
        boolean flag = false
        _ids.each {_id->
            flag = actionExpenditureService.deleteOne(_id)
        }
        ResultUtil.recallAjax(flag)
    }

    /**
     * 修改支出项目
     * @param _id
     * @param name
     * @return
     */
    @RequestMapping("updateExpend")
    @ResponseBody
    def updateExpend(String _id, String name, String item){
        Map<String,DBObject> actionInfo = [_id:_id,name:name,item: item]
        DBObject dbObject = ToDBObjectUtil.toDBObject(actionInfo)
        boolean flag = actionExpenditureService.updateOne(_id,dbObject)
        ResultUtil.recallAjax(flag)
    }




    /*************方法模块***************/

    def saveIncome(DBObject actionInfo){
        ArgsUtil.checkArgs(actionInfo)
        //判断角色是否存在
        DBObject income_db = actionIncomeService.findOneById(actionInfo._id)
        if (income_db == null) {
            actionIncomeService.insertOne(actionInfo)
            return true
        }else {
            return false
        }

    }

    def saveExpend(DBObject actionInfo){
        ArgsUtil.checkArgs(actionInfo)
        //判断角色是否存在
        DBObject expend_db = actionExpenditureService.findOneById(actionInfo._id)
        if (expend_db == null) {
            actionExpenditureService.insertOne(actionInfo)
            return true
        }else {
            return false
        }

    }

}
